package pack;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {
    private Socket socket = null;

    public TCPClient(String serverIP, int serverPort) throws IOException {
        socket = new Socket(serverIP, serverPort);
    }

    public void start(){
        System.out.println("客户端启动!");
        Scanner scanner = new Scanner(System.in);
     try (InputStream inputStream = socket.getInputStream();
          OutputStream outputStream = socket.getOutputStream()) {
         while (true) {
             System.out.println(">");
             String request = scanner.next();
             if (request.equals("exit")) {
                 System.out.println("good bye");
                 break;
             }
             PrintWriter printWriter = new PrintWriter(outputStream);
             printWriter.println(request);
             printWriter.flush();
             Scanner resScanner = new Scanner(inputStream);
             String response = resScanner.next();
             System.out.println(response);
         }
     }catch (IOException e){
         e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TCPClient client = new TCPClient("127.0.0.1", 9090);
        client.start();
    }
}
